public class Demo1_6 {
    //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
    public static void main(String[] args) {
        //方法1
        int i = 0;
        int j = 1;
        double sum = 0;
        for (i = 1; i < 101; i++) {
            sum += (1.0 / i) * j;
            j = -j;
        }
        System.out.println(sum);
    }

    public static void main1(String[] args) {
        //方法2
        int i = 0;
        double sum = 0;
        double ret = 0;
        for (i = 1; i <= 100; i += 2) {
            sum += (1.0 / i);
        }
        for (i = 2; i <= 100; i += 2) {
            ret += (1.0 / i);
        }
        double a = sum - ret;
        System.out.println(a);
    }
}
